package com.zzptc.easyshop.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet(name = "LonginCookieServlet")
public class LonginCookieServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=request.getParameter("name");
        String pwd=request.getParameter("pwd");
        HttpSession session=request.getSession();
//        if(request.getCookies()!=null) {
//            //读取Cookie对象
//            Cookie[] co = request.getCookies();
//            //迭代遍历
//            for(Cookie c:co){
//                if(c.getName().equals("username")){
//
//                    session.setAttribute("loginname",c.getValue());
//                }
//                if(c.getName().equals("password")){
//                    session.setAttribute("loginpwd",c.getValue());
//                }
//            }
//        }else {
            //创建Cookie对象
            Cookie cookie = new Cookie("username", name);
            Cookie cookie1 = new Cookie("password", pwd);
            //设置生命周期   以秒为单位
            cookie.setMaxAge(10 * 24 * 60 * 60);//（保存10天）
            cookie1.setMaxAge(10 * 24 * 60 * 60);
            //往客户端写
            response.addCookie(cookie);
            response.addCookie(cookie1);
//        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
